package com.sky.task;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

@Component
@Slf4j
//Spring Task定时任务框架
@EnableScheduling
//cron表达式 秒 分 时 日 月 周 年（可选），类型是一个字符串，多个值用逗号隔开，*表示所有值，?表示不指定值，-表示范围，/表示步长，L表示最后一天，W表示工作日，#表示指定第几个星期
//cron.qqe2.com表达数学表达式
public class Task {
    @Autowired
    private OrderMapper orderMapper;
    //每秒运行一次
    @Scheduled(cron = "0 0/5 * * * ? ")
    public void updateOrderStatus(){
        List<Orders> ordersList = orderMapper.getOedersAll();
        for (Orders orders : ordersList) {
            LocalDateTime orderTime = orders.getOrderTime();
            LocalDateTime now = LocalDateTime.now();
            if (now.minusMinutes(30).isAfter(orderTime) && orders.getStatus() == Orders.CONFIRMED) {
                orderMapper.updateStatus3(orders.getId(), Orders.DELIVERY_IN_PROGRESS);
            }
        }
    }
    //派送超时的订单
    @Scheduled(cron = "0 0/10 * * * ? ")
    public void updateOrderStatus2(){
        List<Orders> ordersList = orderMapper.getOedersAll();
        for (Orders orders : ordersList) {
            LocalDateTime orderTime = orders.getOrderTime();
            LocalDateTime now = LocalDateTime.now();

            // 判断当前时间是否在下单时间50分钟后
            if (now.isAfter(orderTime.plusMinutes(50))) {
                orderMapper.updateStatus5(orders.getId(),5);
            }
        }
    }

}
